﻿<template>
	<div class="card-fill layout-padding" v-loading="loading || ocrLoading" element-loading-text="Loading...">
		<el-tabs v-model="activeTab">
			<el-tab-pane label="基本信息" name="main">
				<el-card shadow="hover" class="layout-padding-auto create-form">
					<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
						<!-- 单据信息 -->
						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>单据信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_BillCode_FormItem" prop="Entity.BillCode">
										<template #label>
											<WtmLabelTooltip label="单据编号" tooltip="单编号" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.BillCode" clearable class="width100" disabled></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Remark_FormItem" prop="Entity.Remark">
										<template #label>
											<WtmLabelTooltip label="备注" tooltip="备注" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Remark" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<!-- 基本信息 -->
						<!-- <div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>基本信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Name_FormItem" prop="Entity.Name" :rules="[{ required: true, message: '姓名为必填项', trigger: 'blur' }]">
										<template #label>
											<WtmLabelTooltip label="姓名" tooltip="员工姓名" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Name" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_StaffNo_FormItem"
										prop="Entity.StaffNo"
										:rules="[{ required: true, message: '工号为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="工号" tooltip="员工工号" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.StaffNo" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_BirthDate_FormItem"
										prop="Entity.BirthDate"
										:rules="[{ required: true, message: '出生日期为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="出生日期" tooltip="出生日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.BirthDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_Gender_FormItem"
										prop="Entity.Gender"
										:rules="[{ required: true, message: '性别为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="性别" tooltip="性别" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.Gender" clearable class="width100">
											<el-option key="男" value="男" label="男"></el-option>
											<el-option key="女" value="女" label="女"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Ethnicity_FormItem" prop="Entity.Ethnicity">
										<template #label>
											<WtmLabelTooltip label="民族" tooltip="民族" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.Ethnicity" clearable class="width100">
											<el-option key="汉族" value="汉族" label="汉族"></el-option>
											<el-option key="回族" value="回族" label="回族"></el-option>
											<el-option key="满族" value="满族" label="满族"></el-option>
											<el-option key="蒙古族" value="蒙古族" label="蒙古族"></el-option>
											<el-option key="壮族" value="壮族" label="壮族"></el-option>
											<el-option key="其他" value="其他" label="其他"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_HighestEducation_FormItem" prop="Entity.HighestEducation">
										<template #label>
											<WtmLabelTooltip label="最高学历" tooltip="最高学历" />
										</template>
									<el-select v-model="stateData.vmModel.Entity.HighestEducation" clearable class="width100">
										<el-option key="小学" value="小学" label="小学"></el-option>
										<el-option key="初中" value="初中" label="初中"></el-option>
											<el-option key="高中" value="高中" label="高中"></el-option>
											<el-option key="中专" value="中专" label="中专"></el-option>
											<el-option key="大专" value="大专" label="大专"></el-option>
											<el-option key="本科" value="本科" label="本科"></el-option>
											<el-option key="研究生" value="研究生" label="研究生"></el-option>
											<el-option key="硕士" value="硕士" label="硕士"></el-option>
											<el-option key="博士" value="博士" label="博士"></el-option>
											<el-option key="其他" value="其他" label="其他"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_MaritalStatus_FormItem" prop="Entity.MaritalStatus">
										<template #label>
											<WtmLabelTooltip label="婚姻状况" tooltip="婚姻状况" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.MaritalStatus" clearable class="width100">
											<el-option key="未婚" value="未婚" label="未婚"></el-option>
											<el-option key="已婚" value="已婚" label="已婚"></el-option>
											<el-option key="离异" value="离异" label="离异"></el-option>
											<el-option key="丧偶" value="丧偶" label="丧偶"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_PhotoId_FormItem" prop="Entity.PhotoId">
										<template #label>
											<WtmLabelTooltip label="照片" tooltip="员工照片文件" />
										</template>
										<wtm-upload-image v-model="stateData.vmModel.Entity.PhotoId" clearable class="width100"></wtm-upload-image>
									</el-form-item>
								</el-col>
							</el-row>
						</div> -->

						<!-- 身份信息 -->
						<!-- <div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>身份信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_IdCardNo_FormItem"
										prop="Entity.IdCardNo"
										:rules="[{ required: true, message: '身份证号为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="身份证号" tooltip="身份证号码" />
										</template>
										<el-input
											v-model="stateData.vmModel.Entity.IdCardNo"
											@blur="parseIdCardInfo"
											clearable
											class="width100"
											disabled
											placeholder="请上传身份证正反面照片"
										></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_IdCardExpiryDate_FormItem" prop="Entity.IdCardExpiryDate">
										<template #label>
											<WtmLabelTooltip label="身份证有效期" tooltip="身份证有效期" />
										</template>
										<el-input
											:value="other.formatIdCardExpiry(stateData.vmModel.Entity.IdCardExpiryDate)"
											clearable
											class="width100"
											disabled
											placeholder="上传身份证国徽面后自动填充"
										></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_IdCardFrontPhotoId_FormItem" prop="Entity.IdCardFrontPhotoId">
										<template #label>
											<WtmLabelTooltip label="身份证人像面" tooltip="身份证人像面文件" />
										</template>
										<wtm-upload-image
											v-model="stateData.vmModel.Entity.IdCardFrontPhotoId"
											clearable
											class="width100"
											@update:model-value="handleUploadRefresh($event, 'front')"
										></wtm-upload-image>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_IdCardBackPhotoId_FormItem" prop="Entity.IdCardBackPhotoId">
										<template #label>
											<WtmLabelTooltip label="身份证国徽面" tooltip="身份证国徽面文件" />
										</template>
										<wtm-upload-image
											v-model="stateData.vmModel.Entity.IdCardBackPhotoId"
											clearable
											class="width100"
											@update:model-value="handleUploadRefresh($event, 'back')"
										></wtm-upload-image>
									</el-form-item>
								</el-col>

								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_HouseholdRegistration_FormItem" prop="Entity.HouseholdRegistration">
										<template #label>
											<WtmLabelTooltip label="户口所在地" tooltip="户口所在地" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.HouseholdRegistration" clearable class="width100" disabled></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_NativePlace_FormItem" prop="Entity.NativePlace">
										<template #label>
											<WtmLabelTooltip label="籍贯" tooltip="籍贯" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.NativePlace" clearable class="width100" disabled></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div> -->

						<!-- 联系方式 -->
						<!-- <div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>联系方式</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_HomeAddress_FormItem" prop="Entity.HomeAddress">
										<template #label>
											<WtmLabelTooltip label="家庭住址" tooltip="家庭住址" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.HomeAddress" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Phone_FormItem" prop="Entity.Phone">
										<template #label>
											<WtmLabelTooltip label="手机号" tooltip="手机号码" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Phone" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Email_FormItem" prop="Entity.Email">
										<template #label>
											<WtmLabelTooltip label="邮箱" tooltip="电子邮箱" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Email" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div> -->

						<!-- 银行账户 -->
						<!-- <div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>银行账户</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_SalaryBank_FormItem" prop="Entity.SalaryBank">
										<template #label>
											<WtmLabelTooltip label="工资卡银行" tooltip="工资卡开户银行" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.SalaryBank" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_SalaryAccount_FormItem" prop="Entity.SalaryAccount">
										<template #label>
											<WtmLabelTooltip label="工资卡号" tooltip="工资卡账号" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.SalaryAccount" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_BonusBank_FormItem" prop="Entity.BonusBank">
										<template #label>
											<WtmLabelTooltip label="奖金卡银行" tooltip="奖金卡开户银行" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.BonusBank" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_BonusAccount_FormItem" prop="Entity.BonusAccount">
										<template #label>
											<WtmLabelTooltip label="奖金卡号" tooltip="奖金卡账号" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.BonusAccount" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div> -->

						<!-- 工作信息 -->
						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>工作信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_OrgId_FormItem"
										prop="Entity.OrgId"
										:rules="[{ required: true, message: '人事组织为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="人事组织" tooltip="关联的人事组织" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.OrgId" clearable class="width100">
											<el-option v-for="item in stateData.AllSysOrgs" :key="item.Value" :value="item.Value" :label="item.Text"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="24" class="mb20">
									<WtmDeptSelector
										v-model:model-value="stateData.vmModel.Entity.GroupId"
										v-model:position-id="stateData.vmModel.Entity.JobPositionId"
										prop="Entity.GroupId"
										position-prop="Entity.JobPositionId"
										label="所属部门"
										position-label="工作岗位"
										:show-position="true"
										:use-row="true"
										:dept-col="12"
										:position-col="12"
									/>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ResumeId_FormItem" prop="Entity.ResumeId">
										<template #label>
											<WtmLabelTooltip label="简历" tooltip="员工简历文件" />
										</template>
										<wtm-upload-file v-model="stateData.vmModel.Entity.ResumeId" clearable class="width100"></wtm-upload-file>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_HireDate_FormItem"
										prop="Entity.HireDate"
										:rules="[{ required: true, message: '入职日期为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="入职日期" tooltip="入职日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.HireDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_EmploymentType_FormItem" prop="Entity.EmploymentType">
										<template #label>
											<WtmLabelTooltip label="用工形式" tooltip="用工形式" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.EmploymentType" clearable class="width100">
											<el-option key="全职" value="全职" label="全职"></el-option>
											<el-option key="兼职" value="兼职" label="兼职"></el-option>
											<el-option key="实习" value="实习" label="实习"></el-option>
											<el-option key="外包" value="外包" label="外包"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ContractType_FormItem" prop="Entity.ContractType">
										<template #label>
											<WtmLabelTooltip label="劳动合同类型" tooltip="劳动合同类型" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.ContractType" clearable class="width100">
											<el-option key="固定期限" value="固定期限" label="固定期限"></el-option>
											<el-option key="无固定期限" value="无固定期限" label="无固定期限"></el-option>
											<el-option key="项目合同" value="项目合同" label="项目合同"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ContractNo_FormItem" prop="Entity.ContractNo">
										<template #label>
											<WtmLabelTooltip label="合同编号" tooltip="合同编号" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.ContractNo" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ContractStartDate_FormItem" prop="Entity.ContractStartDate">
										<template #label>
											<WtmLabelTooltip label="合同起始日" tooltip="劳动合同起始日" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.ContractStartDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ContractEndDate_FormItem" prop="Entity.ContractEndDate">
										<template #label>
											<WtmLabelTooltip label="合同终止日" tooltip="劳动合同终止日" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.ContractEndDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_State_FormItem" prop="Entity.State">
										<template #label>
											<WtmLabelTooltip label="人员状态" tooltip="人员状态" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.State" clearable class="width100">
											<el-option key="正常" value="正常" label="正常"></el-option>
											<el-option key="停用" value="停用" label="停用"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<!-- 其他信息 -->
						<!-- <div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>其他信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_BloodType_FormItem" prop="Entity.BloodType">
										<template #label>
											<WtmLabelTooltip label="血型" tooltip="血型" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.BloodType" clearable class="width100">
											<el-option key="A" value="A" label="A"></el-option>
											<el-option key="B" value="B" label="B"></el-option>
											<el-option key="AB" value="AB" label="AB"></el-option>
											<el-option key="O" value="O" label="O"></el-option>
											<el-option key="其他" value="其他" label="其他"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Hobbies_FormItem" prop="Entity.Hobbies">
										<template #label>
											<WtmLabelTooltip label="兴趣爱好特长" tooltip="兴趣爱好及特长" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Hobbies" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div> -->

						<!-- 入职准备 -->
						<!-- <div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>入职准备</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_PersonMedicalId_FormItem" prop="Entity.PersonMedicalId">
										<template #label>
											<WtmLabelTooltip label="入职体检" tooltip="关联体检记录实体" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.PersonMedicalId" clearable class="width100">
											<el-option v-for="item in stateData.AllSysPersonMedicals" :key="item.Value" :value="item.Value" :label="item.Text"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_SysPersonTrainExamId_FormItem" prop="Entity.SysPersonTrainExamId">
										<template #label>
											<WtmLabelTooltip label="入职培训" tooltip="关联培训记录实体" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.SysPersonTrainExamId" clearable class="width100">
											<el-option
												v-for="item in stateData.AllSysPersonTrainExams"
												:key="item.Value"
												:value="item.Value"
												:label="item.Text"
											></el-option>
										</el-select>
									</el-form-item>
								</el-col>
							</el-row>
						</div> -->

						<!-- 注释掉的字段区域 -->
						<!-- 
						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>政治面貌信息</span>
							</div>
							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_PoliticalStatus_FormItem" prop="Entity.PoliticalStatus">
										<template #label>
											<WtmLabelTooltip label="政治面貌" tooltip="政治面貌" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.PoliticalStatus" clearable class="width100">
											<el-option key="群众" value="群众" label="群众"></el-option>
											<el-option key="共青团员" value="共青团员" label="共青团员"></el-option>
											<el-option key="中共党员" value="中共党员" label="中共党员"></el-option>
											<el-option key="无党派人士" value="无党派人士" label="无党派人士"></el-option>
											<el-option key="其他" value="其他" label="其他"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_PartyJoinDate_FormItem" prop="Entity.PartyJoinDate">
										<template #label>
											<WtmLabelTooltip label="入党(团)日期" tooltip="入党或入团日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.PartyJoinDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_PartyBranch_FormItem" prop="Entity.PartyBranch">
										<template #label>
											<WtmLabelTooltip label="入党所在支部" tooltip="入党所在支部" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.PartyBranch" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ArchiveLocation_FormItem" prop="Entity.ArchiveLocation">
										<template #label>
											<WtmLabelTooltip label="档案所在地" tooltip="档案存放地" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.ArchiveLocation" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ArchiveNo_FormItem" prop="Entity.ArchiveNo">
										<template #label>
											<WtmLabelTooltip label="存档号" tooltip="档案编号" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.ArchiveNo" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ArchiveTransferOutDate_FormItem" prop="Entity.ArchiveTransferOutDate">
										<template #label>
											<WtmLabelTooltip label="档案调出日期" tooltip="档案调出日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.ArchiveTransferOutDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ArchiveTransferInDate_FormItem" prop="Entity.ArchiveTransferInDate">
										<template #label>
											<WtmLabelTooltip label="档案调入日期" tooltip="档案调入日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.ArchiveTransferInDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>其他信息</span>
							</div>
							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_BloodType_FormItem" prop="Entity.BloodType">
										<template #label>
											<WtmLabelTooltip label="血型" tooltip="血型" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.BloodType" clearable class="width100">
											<el-option key="A" value="A" label="A"></el-option>
											<el-option key="B" value="B" label="B"></el-option>
											<el-option key="AB" value="AB" label="AB"></el-option>
											<el-option key="O" value="O" label="O"></el-option>
											<el-option key="其他" value="其他" label="其他"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Hobbies_FormItem" prop="Entity.Hobbies">
										<template #label>
											<WtmLabelTooltip label="兴趣爱好特长" tooltip="兴趣爱好及特长" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Hobbies" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div>
						-->

						<el-row :gutter="20"></el-row>
					</el-form>
				</el-card>
			</el-tab-pane>
			<!-- <el-tab-pane label="学习经历" name="personeducations" v-if="stateData.vmModel.Entity.ID">
				<SysPersonEducation :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="家庭成员" name="personfamilies" v-if="stateData.vmModel.Entity.ID">
				<SysPersonFamily :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="工作经历" name="personworks" v-if="stateData.vmModel.Entity.ID">
				<SysPersonWork :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="培训考试" name="persontrainexams" v-if="stateData.vmModel.Entity.ID">
				<SysPersonTrainExam :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="员工体检" name="personmedicals" v-if="stateData.vmModel.Entity.ID">
				<SysPersonMedical :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="员工履历" name="personresumes" v-if="stateData.vmModel.Entity.ID">
				<SysPersonResume :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="员工合同" name="personcontracts" v-if="stateData.vmModel.Entity.ID">
				<SysPersonContract :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane> -->
		</el-tabs>
	</div>
</template>

<script setup lang="ts" name="message._system.common.vm.add;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, computed, nextTick, watch, type ComponentInternalInstance } from 'vue';
import { SysHrOnboardingRequestBillApi as api } from '/@/views/hr/syshronboardingrequestbill/api';
import { commonApi } from '/@/api/common';
import other from '/@/utils/other';
import { ISysHrOnboardingRequestBillVm } from '/@/views/hr/syshronboardingrequestbill/api/interface';
import { IViewModel } from '/@/types/common';
import { useIdCardOcr } from '/@/hooks/useIdCardOcr';
import { useIdCardParser } from '/@/hooks/useCardParser';

import SysPersonEducation from '/@/views/person/syspersoneducation/index.vue';
import SysPersonFamily from '/@/views/person/syspersonfamily/index.vue';
import SysPersonWork from '/@/views/person/syspersonwork/index.vue';
import SysPersonTrainExam from '/@/views/person/syspersontrainexam/index.vue';
import SysPersonMedical from '/@/views/person/syspersonmedical/index.vue';
import SysPersonResume from '/@/views/person/syspersonresume/index.vue';
import SysPersonContract from '/@/views/person/syspersoncontract/index.vue';

// 常量定义
const BILL_STATUS = {
	DRAFT: '草稿',
	FORMAL: '正文',
} as const;

const WORKFLOW_STATUS = {
	PENDING: '待提交',
	SUBMITTING: '提交中',
	REVIEWING: '审核中',
	COMPLETED: '已完成',
	CANCELLED: '已取消',
} as const;

const BUTTON_TEXTS = {
	SAVE: '保存',
	SUBMIT: '提交',
	CLOSE: '关闭',
} as const;

const API_ENDPOINTS = {
	PERSON_MEDICALS: '/api/SysHrOnboardingRequestBill/GetSysPersonMedicals',
	PERSON_TRAIN_EXAMS: '/api/SysHrOnboardingRequestBill/GetSysPersonTrainExams',
	ORGS: '/api/Common/GetOrganizations',
} as const;

const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const hasSavedOnce = ref(false);

// 初始化身份证OCR hooks
const { recognizeIdCard, loading: ocrLoading } = useIdCardOcr({
	autoFill: true,
	showMessage: true,
});

// 使用身份证解析器
const { parseAndFill } = useIdCardParser();

const stateData = reactive<IViewModel<ISysHrOnboardingRequestBillVm>>({
	vmModel: {
		Entity: {
			PersonMedicalId: null,
			SysPersonTrainExamId: null,
			BillCode: null,
			BillStatus: null,
			WorkflowStatus: null,
			Remark: null,
			Approver: null,
			ApprovalTime: null,
			Name: null,
			StaffNo: null,
			BirthDate: null,
			Age: null,
			Gender: null,
			Ethnicity: null,
			HighestEducation: null,
			MaritalStatus: null,
			PhotoId: null,
			IdCardNo: null,
			IdCardFrontPhotoId: null,
			IdCardBackPhotoId: null,
			IdCardExpiryDate: null,
			HouseholdRegistration: null,
			NativePlace: null,
			PoliticalStatus: null,
			PartyJoinDate: null,
			PartyBranch: null,
			ArchiveLocation: null,
			ArchiveNo: null,
			ArchiveTransferOutDate: null,
			ArchiveTransferInDate: null,
			HomeAddress: null,
			Phone: null,
			Email: null,
			SalaryBank: null,
			SalaryAccount: null,
			BonusBank: null,
			BonusAccount: null,
			OrgId: null,
			GroupId: null,
			JobPositionId: null,
			ResumeId: null,
			JoinWorkDate: null,
			HireDate: null,
			CompanyYears: null,
			WorkYears: null,
			ConfirmationDate: null,
			EmploymentType: null,
			ContractType: null,
			ContractNo: null,
			ContractStartDate: null,
			ContractEndDate: null,
			ResignationDate: null,
			ResignationReason: null,
			ResignationType: null,
			State: null,
			BloodType: null,
			Hobbies: null,
			ExpectedStartDate: null,
		},
		ActionName: null,
		IsMyApprove: false,
		Remark: null,
	},

	AllSysPersonMedicals: [] as any[],
	AllSysPersonTrainExams: [] as any[],
	AllSysOrgs: [] as any[],
	AllSysGroups: [] as any[],
	AllSysJobPositions: [] as any[],
});

// 计算属性
const hasEntityId = computed(() => {
	const entity = stateData.vmModel.Entity as any;
	return !!(entity && entity.ID);
});

const canSubmit = computed(() => hasEntityId.value && hasSavedOnce.value);

// 统一的成功处理函数
const handleSuccess = (response: any, isSubmit = false) => {
	// 根据操作类型显示不同的消息
	if (isSubmit) {
		ElMessage.success(ci.proxy?.$t('message._system.common.vm.submittip') || '提交成功');
		emit('refresh');
		closeDialog();
	} else {
		ElMessage.success(ci.proxy?.$t('message._system.common.vm.savetip') || '保存成功');
		emit('refresh');
		// 保存后不关闭弹窗，更新实体ID
		if (response?.Entity?.ID) {
			stateData.vmModel.Entity = response.Entity;
		} else if (response?.ID) {
			(stateData.vmModel.Entity as any).ID = response.ID;
		}
		hasSavedOnce.value = true;
	}

	loading.value = false;
	nextTick(() => {
		setupHeaderButtons();
	});
};

// 统一的错误处理函数
const handleError = (error: any) => {
	other.setFormError(ci, error);
	loading.value = false;
};

// 公共的API请求处理函数
const handleApiRequest = (apiCall: () => Promise<any>, beforeSubmit: () => void, isSubmit = false) => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			beforeSubmit();
			apiCall()
				.then((response: any) => handleSuccess(response, isSubmit))
				.catch(handleError);
		}
	});
};

// 取消
const onClose = () => {
	closeDialog();
};

// 保存
const onSave = () => {
	handleApiRequest(
		() => {
			// 创建一个深拷贝，确保新增时不发送无效的 ID 字段
			const submitData = JSON.parse(JSON.stringify(stateData.vmModel));
			if (!submitData.Entity.ID) {
				// 如果没有有效的 ID，删除该字段
				delete submitData.Entity.ID;
			}
			return api().add(submitData);
		},
		() => {
			stateData.vmModel.Entity.BillStatus = BILL_STATUS.DRAFT;
		},
		false // 保存后不关闭弹窗
	);
};

// 提交
const onSubmit = () => {
	handleApiRequest(
		() => api().edit(stateData.vmModel),
		() => {
			stateData.vmModel.Entity.WorkflowStatus = WORKFLOW_STATUS.SUBMITTING;
		},
		true // 提交后关闭弹窗
	);
};

const activeTab = ref('main');

// 身份证上传识别处理
const handleUploadRefresh = async (value: string, type: 'front' | 'back') => {
	if (!value) return;
	await recognizeIdCard(value, type, stateData.vmModel.Entity);
};

// 解析身份证号码信息
const parseIdCardInfo = () => {
	if (stateData.vmModel.Entity.IdCardNo) {
		parseAndFill(stateData.vmModel.Entity.IdCardNo, stateData.vmModel.Entity);
	}
};

// 计算工龄
watch(
	() => stateData.vmModel.Entity.JoinWorkDate,
	(newJoinWorkDate) => {
		if (newJoinWorkDate) {
			stateData.vmModel.Entity.WorkYears = other.calculateWorkYears(newJoinWorkDate).decimalYears;
		}
	}
);

// 计算司龄
watch(
	() => stateData.vmModel.Entity.HireDate,
	(newHireDate) => {
		if (newHireDate) {
			const companyYearsResult = other.calculateWorkYears(newHireDate);
			stateData.vmModel.Entity.CompanyYears = companyYearsResult.decimalYears;
		} else {
			stateData.vmModel.Entity.CompanyYears = null;
		}
	}
);

// 初始化页面数据
const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
		hasSavedOnce.value = true;
	}
};

// 获取当前登录人信息并回显所有数据
const getCurrentPersonInfo = async () => {
	try {
		const response = await commonApi().getPersonInfo({});
		if (response && response.Data) {
			const personInfo = response.Data;
			stateData.vmModel.Entity = personInfo;
		}
	} catch (error) {
		console.warn('获取当前登录人信息失败:', error);
	}
};

// 加载页面数据
const loadPageData = async () => {
	const [personMedicals, personTrainExams, orgs, billCode, personInfo] = await Promise.allSettled([
		other.getSelectList(API_ENDPOINTS.PERSON_MEDICALS, [], false),
		other.getSelectList(API_ENDPOINTS.PERSON_TRAIN_EXAMS, [], false),
		other.getSelectList(API_ENDPOINTS.ORGS, [], false),
		api().getBillCode(),
		getCurrentPersonInfo(),
	]);

	// 处理成功的结果
	if (personMedicals.status === 'fulfilled') stateData.AllSysPersonMedicals = personMedicals.value;
	if (personTrainExams.status === 'fulfilled') stateData.AllSysPersonTrainExams = personTrainExams.value;
	if (orgs.status === 'fulfilled') stateData.AllSysOrgs = orgs.value;
	if (billCode.status === 'fulfilled') stateData.vmModel.Entity.BillCode = billCode.value.Data as string;
};

// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		await loadPageData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.SAVE,
			type: undefined,
			onClick: onSave,
			visible: !hasSavedOnce.value, // 未保存过时显示保存按钮
		},
		{
			text: BUTTON_TEXTS.SUBMIT,
			confirm: '提交单据以后无法修改，是否确认提交?',
			type: 'primary',
			onClick: onSubmit,
			visible: canSubmit.value, // 已保存且有ID时显示提交按钮
		},
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];

	emit('updateHeaderButtons', headerButtons);
};
</script>
